package com.chancingpack.service.impl;

import com.chancingpack.dal.dao.MyProductDao;
import com.chancingpack.service.MyProductService;
import com.chancingpack.util.ImageLoadUtil;
import com.chancingpack.util.PageSizeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/7/21.
 */
@Service
public class MyProductServiceImpl implements MyProductService {

    @Autowired
    private MyProductDao myProductDao;

    /**
     * 查询所有产品信息
     * @param request
     * @return
     */
    public List<Map<String, Object>> selectPictures(Map<String,Object> map,HttpServletRequest request) {
        if ("".equals(map.get("pageNumber")) || map.get("pageNumber") == null) {
            map.put("pageNumber",1);
        }
        //设置每页查询条数
        map.put("pageSize", PageSizeUtil.PAGESIZE);
        Integer pageNumber = (Integer) map.get("pageNumber");
        //设置每页查询起始数
        map.put("pageStart", PageSizeUtil.PAGESIZE * (pageNumber - 1));
        List<Map<String, Object>> mapList = myProductDao.selectPictures(map);
        if (mapList.size() > 0) {
            for (Map<String, Object> mapObj: mapList) {
                String archId = (String) mapObj.get("ARCH_ID");
                //获取相应产品y压缩图片Url,通过archId将它放入map中
                String compressUrl = request.getContextPath() + "/resources/upload/compress/" + archId +".jpg";
                mapObj.put("compressImage",compressUrl);
                //获取相应产品原图片Url,通过archId将它放入map中
                String originalUrl = request.getContextPath() + "/resources/upload/original/" + archId +".jpg";
                mapObj.put("originalImage",originalUrl);
                //获取相应产品外码，如果为null，给他复制空串。
                String prodExter = (String) mapObj.get("prodExter");
                if (prodExter == null) {
                    mapObj.put("prodExter","");
                }
            }
        }
        return mapList;
    }

    public List<Map<String, Object>> selectBycustId(Map<String, Object> map) {
        return myProductDao.selectBycustId(map);
    }
}
